﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Blabber.Data;
using System.Data;

namespace Blabber.Client
{
    /// <summary>
    /// Interaction logic for WindowSettings.xaml
    /// </summary>
    public partial class WindowSettings : Window
    {
        Blabber.Data.DataSetModel.ConnectionDataTable _connections;
 
        public WindowSettings()
        {
            InitializeComponent();

            this.Closed += new EventHandler(WindowSettings_Closed);

            this.Left = Blabber.Client.Properties.Settings.Default.SettingsWindowLocation.X;
            this.Top = Blabber.Client.Properties.Settings.Default.SettingsWindowLocation.Y;
        }

        void WindowSettings_Closed(object sender, EventArgs e)
        {
            Blabber.Client.Properties.Settings.Default.SettingsWindowLocation = new System.Drawing.Point((int)this.Left, (int)this.Top);
        }

        public Blabber.Data.DataSetModel.ConnectionDataTable Connections
        {
            get { return _connections; }
            set 
            { 
                _connections = value;
                connectionListView.DataContext = _connections;
                detailGrid.DataContext = _connections;
                _connections.RowChanged += new System.Data.DataRowChangeEventHandler(_connections_RowChanged);
            }
        }

        void _connections_RowChanged(object sender, System.Data.DataRowChangeEventArgs e)
        {
            if ((e.Action != DataRowAction.Add) && (e.Action != DataRowAction.Delete))
            {
                _connections.RowChanged -= _connections_RowChanged;
                try
                {
                    Blabber.Data.DataSetModel.ConnectionRow row = (e.Row as Blabber.Data.DataSetModel.ConnectionRow);
                    row.JID = string.Format("{0}@{1}", row.UserName, row.Server);
                }
                finally
                {
                    _connections.RowChanged += _connections_RowChanged;
                }
            }
        }

        private void ButtonNewConnection_Click(object sender, RoutedEventArgs e)
        {
            Blabber.Data.DataSetModel.ConnectionRow row = _connections.NewConnectionRow();
            row.Server = "gmail.com";
            row.ConnectServer = "talk.google.com";
            row.Port = 5222;
            row.UserName = "";
            row.Password = "";
            row.JID = string.Format("{0}@{1}", row.UserName, row.Server);

            _connections.AddConnectionRow(row);

            connectionListView.SelectedItem = row;

            textBoxUserName.Focus();
        }

        private void ButtonOK_Click(object sender, RoutedEventArgs e)
        {

        }

        private void ButtonSetPassword_Click(object sender, RoutedEventArgs e)
        {
            Blabber.Data.DataSetModel.ConnectionRow row = (connectionListView.SelectedItem as DataRowView).Row as Blabber.Data.DataSetModel.ConnectionRow;
            if (null != row)
            {
                row.Password = passwordBox.Password;
            }
        }

        private void DeleteConnection_Click(object sender, RoutedEventArgs e)
        {
            if (null != connectionListView.SelectedItem)
            {
                Blabber.Data.DataSetModel.ConnectionRow row = (connectionListView.SelectedItem as DataRowView).Row as Blabber.Data.DataSetModel.ConnectionRow;
                if (null != row)
                {
                    _connections.RemoveConnectionRow(row);
                }
            }
        }
    }
}
